home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / haeberli / libgutil / rct.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  253 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    rct -
  19.  *        Management of rectangles.
  20.  *
  21.  *    rectangles are half open.  that is if xmin is 0 and xmax is 1,
  22.  *    the width of the rectangle is 1.  You can think of the min and
  23.  *    may values representing the locations of the edges.
  24.  *
  25.  *                Paul Haeberli - 1986
  26.  */
  27. #include "rct.h"
  28. #include "port.h"
  29.  
  30. rct *rctnew()
  31. {
  32.     rct *r; 
  33.  
  34.     r = (rct *)mymalloc(sizeof(rct));
  35.     r->xmin = 0;
  36.     r->ymin = 0;
  37.     r->xmax = 0;
  38.     r->ymax = 0;
  39.     return r;
  40. }
  41.  
  42. rctfree(r)
  43. rct *r;
  44. {
  45.     free(r);
  46. }
  47.  
  48. rct *rctclone(r)
  49. rct *r;
  50. {
  51.      rct *c; 
  52.  
  53.      c = rctnew();
  54.      *c = *r;
  55.      return c;
  56. }
  57.  
  58. rctcopy(scr,dst)
  59. rct *scr, *dst;
  60. {
  61.      *dst = *scr;
  62. }
  63.  
  64. rctset(r,x1,y1,x2,y2) 
  65. rct *r; 
  66. int x1, y1, x2, y2;
  67. {
  68.     r->xmin = x1;
  69.     r->ymin = y1;
  70.     r->xmax = x2;
  71.     r->ymax = y2;
  72.     rctnormal(r);
  73. }
  74.  
  75. rctsize(r,ox,oy,sizex,sizey) 
  76. rct *r; 
  77. int ox, oy;
  78. int sizex, sizey;
  79. {
  80.     r->xmin = ox;
  81.     r->ymin = oy;
  82.     r->xmax = ox+sizex;
  83.     r->ymax = oy+sizey;
  84.     rctnormal(r);
  85. }
  86.  
  87. rctnormal(r) 
  88. rct *r; 
  89. {
  90.     int temp;
  91.  
  92.     if(r->xmin > r->xmax) {
  93.     temp = r->xmin;
  94.     r->xmin = r->xmax;
  95.     r->xmax = temp;
  96.     }
  97.     if(r->ymin > r->ymax) {
  98.     temp = r->ymin;
  99.     r->ymin = r->ymax;
  100.     r->ymax = temp;
  101.     }
  102. }
  103.  
  104. rctoffset(r,dx,dy) 
  105. rct *r; 
  106. int dx, dy;
  107. {
  108.     r->xmin += dx;
  109.     r->ymin += dy;
  110.     r->xmax += dx;
  111.     r->ymax += dy;
  112. }
  113.  
  114. rctshrink(r,dx,dy)
  115. rct *r; 
  116. int dx, dy;
  117. {
  118.     r->xmin += dx;
  119.     r->ymin += dy;
  120.     r->xmax -= dx;
  121.     r->ymax -= dy;
  122.     rctnormal(r);
  123. }
  124.  
  125. int rctarea(r)
  126. rct *r; 
  127. {
  128.     return (r->xmax-r->xmin)*(r->ymax-r->ymin);
  129. }
  130.  
  131. int rctinter(src1,src2,dest)
  132. rct *src1, *src2, *dest;
  133. {
  134.     int xmin, xmax;
  135.     int ymin, ymax;
  136.  
  137.     xmin = MAX(src1->xmin,src2->xmin);
  138.     xmax = MIN(src1->xmax,src2->xmax);
  139.     ymin = MAX(src1->ymin,src2->ymin);
  140.     ymax = MIN(src1->ymax,src2->ymax);
  141.     if(xmax>xmin && ymax>ymin) {
  142.     dest->xmin = xmin;
  143.     dest->xmax = xmax;
  144.     dest->ymin = ymin;
  145.     dest->ymax = ymax;
  146.     return 1;
  147.     } else {
  148.     dest->xmin = 0;
  149.     dest->xmax = 0;
  150.     dest->ymin = 0;
  151.     dest->ymax = 0;
  152.     return 0;
  153.     }
  154. }
  155.  
  156. rctunion(src1,src2,dest)
  157. rct *src1, *src2, *dest;
  158. {
  159.     int xmin, xmax;
  160.     int ymin, ymax;
  161.  
  162.     xmin = MIN(src1->xmin,src2->xmin);
  163.     xmax = MAX(src1->xmax,src2->xmax);
  164.     ymin = MIN(src1->ymin,src2->ymin);
  165.     ymax = MAX(src1->ymax,src2->ymax);
  166.     dest->xmin = xmin;
  167.     dest->xmax = xmax;
  168.     dest->ymin = ymin;
  169.     dest->ymax = ymax;
  170. }
  171.  
  172. /* 
  173.  *    return 1 if r1 is entirely inside r2
  174.  *
  175.  */
  176. int rctinrct(r1,r2)
  177. rct *r1, *r2;
  178. {
  179.     rct intr;
  180.  
  181.     rctinter(r1,r2,&intr);
  182.     if(rctarea(&intr) == rctarea(r1))
  183.     return 1;
  184.     else
  185.     return 0;
  186. }
  187.  
  188. int rctinside(r,x,y)
  189. rct *r;
  190. int x, y;
  191. {
  192.     if ( x >= r->xmin && x < r->xmax &&
  193.          y >= r->ymin && y < r->ymax)
  194.     return 1;
  195.     else
  196.     return 0;
  197. }
  198.  
  199. int rctfinside(r,x,y)
  200. rct *r;
  201. float x, y;
  202. {
  203.     if ( x >= r->xmin && x < r->xmax &&
  204.          y >= r->ymin && y < r->ymax)
  205.     return 1;
  206.     else
  207.     return 0;
  208. }
  209.  
  210. int rctequal(r1,r2)
  211. rct *r1, *r2;
  212. {
  213.     if ( r1->xmin == r2->xmin && r1->xmax == r2->xmax &&
  214.                r1->ymin == r2->ymin && r1->ymax == r2->ymax )
  215.     return 1;
  216.     else
  217.     return 0;
  218. }
  219.  
  220. int rctempty(r) 
  221. rct *r;
  222. {
  223.     if(r->xmin == r->xmax || r->ymin == r->ymax)
  224.     return 1;
  225.     else
  226.     return 0;
  227. }
  228.  
  229. rctcenter(r,xcent,ycent)
  230. rct *r;
  231. int *xcent, *ycent;
  232. {
  233.     *xcent = (r->xmin+r->xmax)/2;
  234.     *ycent = (r->ymin+r->ymax)/2;
  235. }
  236.  
  237. rctprint(r)
  238. rct *r;
  239. {
  240.     fprintf(stderr,"xy min %d %d xy max: %d %d\n",
  241.            r->xmin,r->ymin,r->xmax,r->ymax);
  242. }
  243.  
  244. rctscale(r,scale)
  245. rct *r;
  246. float scale;
  247. {
  248.     r->xmin = r->xmin*scale;
  249.     r->ymin = r->ymin*scale;
  250.     r->xmax = r->xmax*scale;
  251.     r->ymax = r->ymax*scale;
  252. }
  253.